<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="form.username" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-button type="primary" class="btn" @click="onSubmit">更新</el-button>
    </el-form>
  </div>
</template>
<script>
import { getUserInfoRequest, updateUserInfoRequest } from '@/api/employees'
export default {
  name: '',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    onSubmit() {
    //   console.log(this.form)
      this.$refs.formRef.validate(v => {
        if (v) {
          this.upadataUserInfo()
        }
      })
    },
    async getUserInfo() {
    //   console.log(this.$route.query.id)
      try {
        const res = await getUserInfoRequest(this.$route.query.id || '')
        // console.log('getUserInfoRequest: ', res)
        if (res.success) {
        //   this.form.username = res.data.username
        //   this.form.password = res.data.password
          this.form = res.data
        }
      } catch (err) {
        console.log('getUserInfoRequest: ', err)
      }
    },
    async upadataUserInfo() {
      try {
        const res = await updateUserInfoRequest(this.form)
        if (res.success) {
          this.$message.success(res.message)
          this.$router.back()
        } else {
          this.$message.success(res.message)
        }
        console.log('updateUserInfoRequest: ', res)
      } catch (err) {
        console.log('updateUserInfoRequest: ', err)
      }
    }
  }
}
</script>
<style lang="scss">
.btn{
    margin-left: 80px;
}
</style>
